/*****************************************************************************
 * Copyright (c) 2010 -, Anders All rights reserved
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *  Contact: Anders Uddenberg (anders.uddenberg@gmail.com)
 * 
 *
 ****************************************************************************/
package org.easypr.organizer.client.view;

import java.util.ArrayList;
import java.util.Date;

import org.easypr.organizer.client.presenter.DashboardPresenter;
import org.easypr.organizer.shared.MyContact;
import org.easypr.organizer.shared.MyMeeting;
import org.zenika.widget.client.datePicker.DatePicker;

import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

// TODO: Auto-generated Javadoc
/**
 * The Class MenuView.
 */
public class DashboardView extends Composite implements DashboardPresenter.Display {

	/** The container. */
	FlowPanel container;
	
	/** The meeting notes. */
	TextArea meetingNotes;
	
	/** The meeting date. */
	TextBox meetingDate;
	
	/** The meeting person id. */
	TextBox meetingPersonId;
	
	/** The meeting button. */
	Button meetingButton;
	
	/** The meeting body. */
	FlexTable meetingBody;
	
	/** The bodycontent. */
	FlexTable bodycontent;
	
	/** The contacts table. */
	FlexTable contactsTable;
	
	/** The selected contact. */
	MyContact selectedContact = null;
	
	/** The create link. */
	Anchor createLink;
	
	/** The import link. */
	Anchor importLink;

	/**
	 * Instantiates a new menu view.
	 */
	public DashboardView() {
		container = new FlowPanel();
		initWidget(container);
		meetingNotes = new TextArea();
		meetingDate = new TextBox();
		meetingButton = new Button("Save");
		meetingBody = new FlexTable();
		bodycontent = new FlexTable();
		contactsTable = new FlexTable();
		createLink = new Anchor("Create one", "#create");
		importLink = new Anchor("import Google Contacts");
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.DashboardPresenter.Display#getMeetingContact()
	 */
	public MyContact getMeetingContact() {
		return this.selectedContact;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.DashboardPresenter.Display#getMeetingDate()
	 */
	public TextBox getMeetingDate() {
		return this.meetingDate;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.DashboardPresenter.Display#getContactsTable()
	 */
	public FlexTable getContactsTable() {
		return this.contactsTable;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.DashboardPresenter.Display#getMeetingNotes()
	 */
	public TextArea getMeetingNotes() {
		return this.meetingNotes;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.DashboardPresenter.Display#getMeetingButton()
	 */
	public Button getMeetingButton() {
		return this.meetingButton;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.DashboardPresenter.Display#getImportLink()
	 */
	public Anchor getImportLink() {
		return this.importLink;
	}
	
	/*
	public FlowPanel getDropdownSearch() {
		return this.dropdownSearch;
	}
	*/
	

	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.DashboardPresenter.Display#setData(java.util.ArrayList)
	 */
	public void setData(ArrayList<MyContact> contacts) {
		
		container.clear();

		HorizontalPanel header = new HorizontalPanel();
		header.setStyleName("content-header");
		
		Label lbl = new Label("Add new meeting");
		lbl.setStyleName("header-label");
		
		header.add(lbl);
		container.add(header);
		
		

		// Contact requests from Friends
		// Friends requests from other Users
		

		bodycontent.setWidth("100%");
		bodycontent.getCellFormatter().setWidth(0, 0, "200px");

		// Add new meeting 
		//bodycontent.setWidget(0, 0, getMeetingWidget());
		Date date = new Date();
		long iTimeStamp = date.getTime();
		FlowPanel contactsWrapper = new FlowPanel();
		contactsWrapper.addStyleName("contacts");
		int row = 0;
		if(contacts != null && contacts.size() > 0) {
			for(MyContact contact : contacts) {
				
				//HorizontalPanel item = new HorizontalPanel();
				
				//Anchor link = new Anchor(contact.getName());
				//link.setTitle(contact.getName());
				
				//item.add(link);
				//Label workplace = new Label(contact.getWorkplace());
				//workplace.addStyleName("contact-workplace");
				//item.add(workplace);
				
				String link = "<a class='gwt-Anchor' href='javascript:;' title='"+contact.getName()+"'>"+contact.getName()+"</a>";
				
				contactsTable.setWidget(row, 0, new HTML(link));
				contactsTable.getCellFormatter().getElement(row, 0).setId(Long.toString(contact.getId()));
				row++;
			}
			
		} else {
			contactsTable.setWidget(0, 0, new Label("No contacts, use the Create link in the menu to add your first!"));
		}
		Date edate = new Date();
        long eTimeStamp = edate.getTime();
        System.out.println(eTimeStamp-iTimeStamp);
		contactsWrapper.add(contactsTable);
		bodycontent.setWidget(0, 0, contactsWrapper);
		
		FlowPanel importPanel = new FlowPanel();
		importPanel.addStyleName("import");
		importPanel.add(new Label("Select a contact to add a new meeting note"));
		importPanel.add(new Label("Missing a contact?"));
		importPanel.add(createLink);
		importPanel.add(new Label("or"));
		importPanel.add(importLink);
		
		bodycontent.setWidget(0, 1, importPanel);
		
		container.add(bodycontent);
		
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.DashboardPresenter.Display#showAddNewMeeting(org.easypr.organizer.shared.MyContact)
	 */
	public void showAddNewMeeting(MyContact contact) {
		this.selectedContact = contact;
		bodycontent.setWidget(0, 1, getMeetingWidget(contact));
		
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.DashboardPresenter.Display#showMeetingSaved(org.easypr.organizer.shared.MyMeeting)
	 */
	public void showMeetingSaved(MyMeeting meeting) {
		Label saved = new Label("Meeting note saved!");
		saved.addStyleName("import");
		bodycontent.setWidget(0, 1, saved);
		
	}
	
	/**
	 * Gets the meeting widget.
	 *
	 * @param contact the contact
	 * @return the meeting widget
	 */
	private FlowPanel getMeetingWidget(MyContact contact) {
		
		/*
		 * Building the widget 
		 */
		FlowPanel meetingWidget = new FlowPanel();
		meetingWidget.addStyleName("widget-large");

		// Header
		HorizontalPanel meetingHeader = new HorizontalPanel();
		meetingHeader.setStyleName("widget-header");
			
		Label meetingLabel = new Label("Add a new meeting note");
		meetingLabel.setStyleName("widget-heading");
			
		meetingHeader.add(meetingLabel);

		// Body
		meetingBody.setStyleName("widget-body");
		meetingBody.setCellSpacing(0);
		meetingBody.setWidth("99%");
		
		
		Label meetingDateLabel = new Label("Select date:");
		meetingBody.setWidget(0, 0, meetingDateLabel);

		
		DatePicker datePicker = new DatePicker();
		//datePicker.setYoungestDate(new Date());
		datePicker.setSelectedDate(new Date());
		meetingBody.setWidget(0,1,datePicker);
		this.meetingDate = datePicker;
		
		meetingButton.setWidth("75px");
		meetingBody.setWidget(0, 2, meetingButton);
		
		Label meetingPersonLabel = new Label("Contact:");
		meetingBody.setWidget(1, 0, meetingPersonLabel);
		Label meetingPerson = new Label(contact.getName());
		meetingBody.setWidget(1, 1, meetingPerson);
		meetingBody.getFlexCellFormatter().setColSpan(1, 1, 2);
		
		meetingNotes.setWidth("100%");
		meetingNotes.setHeight("150px");
		meetingBody.getFlexCellFormatter().setColSpan(2, 0, 3);
		meetingBody.setWidget(2, 0, meetingNotes);
		
		// Assemble the Widget
		meetingWidget.add(meetingHeader);
		meetingWidget.add(meetingBody);
		
		return meetingWidget;
	}
	
	/* (non-Javadoc)
	 * @see org.easypr.organizer.client.presenter.DashboardPresenter.Display#asWidget()
	 */
	@Override
	public Widget asWidget() {
		return this;
	}

}
